﻿                                                     
$(function () {
    Show();
});
function Show() {
 layui.use(function () {
        var table = layui.table;
        var Num = $("#Num").val();
        var Addtime = $("#Date").val();
     console.log(Num)
     console.log(Num)
        // 先进行表格的初始渲染
        table.render({
            elem: '#carriertable',  // 表格元素的选择器
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['exports'],
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'gualityProjectName', title: '质检项目名称' },
                { field: 'gualityProjectNumbering', title: '质检项目编号' },
                { field: 'gualityProjectTool', title: '质检方法' },
                { field: 'gualityProjectMethod', title: '质检工具' },
                { field: 'gualityProjectType', title: '质检类型' },
                { field: 'gualityProjectStandardValue', title: '标准值' },
                {
                    field: 'addTime', title: '添加时间', templet: function (d) {
                        return formatTime(d.addTime); // 调用自定义的时间格式转换函数
                    }
                },
                { field: 'addName', title: '创建人' },
                { field: 'gualityProjectRemark', title: '备注' },
                {
                    field: '', title: '操作', templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.gualityProjectId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.gualityProjectId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }

                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });
        // 创建表格实例
        $.ajax({
            url: "/GualityProject/GualityProjectShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 50,
                gualityProjectNumbering: Num,
                addTime: Addtime,
                token: localStorage.getItem("token")
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('carriertable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
        });
}


window.deleteData = function (id) {
    console.log(id);
    layer.confirm('确定要删除该条数据吗？', function (index) {
        $.ajax({
            type: "Post",
            url: "/GualityProject/GualityProjectDelete",  // 替换为实际的后台删除数据接口地址
            data: { data: JSON.stringify({ gualityProjectId: id }), token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                if (res.data >= 1) {
                    layer.alert("删除成功");
                    Show();  // 重新加载数据，刷新表格显示
                } else {
                    layer.alert("删除失败");

                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }
        });
        layer.close(index);
    });
};

layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    // 打开添加弹窗的按钮点击事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                title: "新增质检项目",
                content: `
                    <form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检项目名称</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectName" autocomplete="off" id="gualityProjectName" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检项目编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectNumbering" id="gualityProjectNumbering" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检工具</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectTool" id="gualityProjectTool" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检方法</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectMethod" id="gualityProjectMethod" required lay-verify="required">
                       <option value="">请选择质检方法</option>
                       <option value="仪器测量">仪器测量</option>
                       <option value="目测量">目测量</option>
                       <option value="尺寸量">尺寸量</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检类型</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectType" id="gualityProjectType" required lay-verify="required">
                       <option value="">请选择</option>
                       <option value="数值">数值</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">标准值</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectStandardValue" id="gualityProjectStandardValue" required lay-verify="required">
                       <option value="">请选择</option>
                       <option value="外观完整">外观完整</option>
                       <option value="按图纸检测">按图纸检测</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                    <textarea name="gualityProjectRemark" id="gualityProjectRemark" placeholder="请输入" class="layui-textarea"></textarea>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">添加</button>
                    </div>
                    <form>`,
                success: function () {

                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field;
                        console.log(field)
                        $.ajax({
                            url: '/GualityProject/GualityProjectAdd',
                            type: 'post',
                            data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                            success: function (res) {
                                var s = JSON.parse(res)
                                console.log(res)
                                if (s.data >= 1) {
                                    layer.alert("添加成功");
                                    setTimeout(function () {
                                        location.reload();
                                    }, 2000);
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });

        }
    })
});
var obj = {
    gualityProjectId: '',
    gualityProjectName: '',
    gualityProjectNumbering: '',
    gualityProjectTool: '',
    gualityProjectMethod: '',
    gualityProjectType: '',
    gualityProjectStandardValue: '',
    gualityProjectRemark:''
}
window.editData = function (id) {
    // 先根据id获取要编辑的数据，发起Ajax请求
    var form = layui.form;
    $.ajax({
        type: "get",
        url: "/GualityProject/GualityProjectModel",  // 替换为实际的后台根据id获取数据的接口地址
        data: { gualityProjectId: id, token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            console.log(res)
            $(res.data).each(function () {
                $('#gualityProjectName').val(this.gualityProjectName)
                $('#gualityProjectNumbering').val(this.gualityProjectNumbering)
                $('#gualityProjectTool').val(this.gualityProjectTool)
                $('#gualityProjectMethod').val(this.gualityProjectMethod)
                $('#gualityProjectType').val(this.gualityProjectType)
                $('#gualityProjectStandardValue').val(this.gualityProjectStandardValue)
                $('#gualityProjectRemark').val(this.gualityProjectRemark)
       form.render('select');
                obj.gualityProjectId = this.gualityProjectId
                obj.gualityProjectName = this.gualityProjectName
                obj.gualityProjectNumbering = this.gualityProjectNumbering
                obj.gualityProjectTool = this.gualityProjectTool
                obj.gualityProjectMethod = this.gualityProjectMethod
                obj.gualityProjectType = this.gualityProjectType
                obj.gualityProjectStandardValue = this.gualityProjectStandardValue
                obj.gualityProjectRemark = this.gualityProjectRemark
                debugger
            })
        },
    });

    layer.open({
        type: 1,
        title: '编辑质检项目',
        content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检项目名称</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectName" autocomplete="off" id="gualityProjectName" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检项目编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectNumbering" id="gualityProjectNumbering" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检工具</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityProjectTool" id="gualityProjectTool" autocomplete="off" placeholder="请输入" class="layui-input">
                    </div>
                    </div>
                    
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检方法</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectMethod" id="gualityProjectMethod" required lay-verify="required">
                       <option value="">请选择质检方法</option>
                       <option value="仪器测量">仪器测量</option>
                       <option value="目测量">目测量</option>
                       <option value="尺寸量">尺寸量</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检类型</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectType" id="gualityProjectType" required lay-verify="required">
                       <option value="">请选择</option>
                       <option value="数值">数值</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">标准值</label>
                    <div class="layui-input-block">
                    <select name="gualityProjectStandardValue" id="gualityProjectStandardValue" required lay-verify="required">
                       <option value="">请选择</option>
                       <option value="外观完整">外观完整</option>
                       <option value="按图纸检测">按图纸检测</option>
                    </select>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                    <textarea name="gualityProjectRemark" id="gualityProjectRemark" placeholder="请输入" class="layui-textarea"></textarea>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                    </div>
                    <form>
                    `,
        area: ['700px', '400px'],
        success: function () {
            var obj1 = {
                gualityProjectId: '',
                gualityProjectName: '',
                gualityProjectNumbering: '',
                gualityProjectTool: '',
                gualityProjectMethod: '',
                gualityProjectType: '',
                gualityProjectStandardValue: '',
                gualityProjectRemark: ''
            };
            console.log(obj1)
            // 对弹层中的表单进行初始化渲染
            form.render();
            // 表单提交事件
            form.on('submit(demo-login)', function (data) {
                var field = data.field;
                obj1.gualityProjectId = obj.gualityProjectId
                obj1.gualityProjectName = field.gualityProjectName
                obj1.gualityProjectNumbering = field.gualityProjectNumbering
                obj1.gualityProjectTool = field.gualityProjectTool
                obj1.gualityProjectMethod = field.gualityProjectMethod
                obj1.gualityProjectType = field.gualityProjectType
                obj1.gualityProjectStandardValue = field.gualityProjectStandardValue
                obj1.gualityProjectRemark = field.gualityProjectRemark
                $.ajax({
                    url: '/GualityProject/GualityProjectUpdate',
                    type: 'post',
                    data: { obj1, token: localStorage.getItem("token") },
                    success: function (res) {
                        var res = JSON.parse(res)
                        console.log(field)
                        console.log(res)
                        if (res.data >= 1) {
                            layer.alert("修改成功");
                            setTimeout(function () {
                                location.reload();
                            }, 2000);
                        }
                        else {
                            layer.alert("修改失败");
                        }
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        }
    });
};

// 定义时间格式转换函数
function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}